package so.sao.code.service.service.impl;

import java.util.List;
import java.util.Vector;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import so.sao.code.dto.request.ThirdPartyCodeCreate;
import so.sao.code.dto.response.ThirdPartyCodeDto;
import so.sao.code.service.domain.entity.TagBatch;
import so.sao.code.service.domain.entity.TagBatchLookupTable;
import so.sao.code.service.repository.TagBatchLookupTableDao;
import so.sao.code.service.service.TagBatchLookupTableService;
import so.sao.code.service.service.TagBatchService;
import so.sao.top.core.util.exceptions.ServiceException;
@Service
public class TagBatchLookupTableServiceImpl implements TagBatchLookupTableService {

	@Autowired
	TagBatchLookupTableDao tagBatchLookupTableDao;
	
	@Autowired
	TagBatchService tagBatchService;
	
	@Override
	public ThirdPartyCodeDto createCodeSegment(ThirdPartyCodeCreate thirdPartyCodeCreate, Long eseId) throws ServiceException,Exception{
		ThirdPartyCodeDto tCodeD = new ThirdPartyCodeDto();
		BeanUtils.copyProperties(thirdPartyCodeCreate, tCodeD);
		Long totalTagCount = thirdPartyCodeCreate.getTagCount();
		List<TagBatchLookupTable> tLookupTables = tagBatchLookupTableDao.findTagBatchLookupTables(eseId,thirdPartyCodeCreate.getSeqType(),totalTagCount);
		TagBatchLookupTable tagBatchLookupTable = null;
		if (tLookupTables.isEmpty()) {
			throw new ServiceException("unavailable tagBatch lookupTable eseId :"+eseId+",type:"+thirdPartyCodeCreate.getSeqType());
		}
		tagBatchLookupTable = tLookupTables.get(0);
		tCodeD.setTagBatchId(tagBatchLookupTable.getTagBatchId());
		tCodeD.setSeqnumStart(tagBatchLookupTable.getSeqnumStart() + tagBatchLookupTable.getCurrentCount());
		tCodeD.setSeqnumEnd(tCodeD.getSeqnumStart() + tCodeD.getTagCount() - 1);
		tCodeD.setSeqType(tagBatchLookupTable.getType().getValue());
		TagBatch tagBatch = tagBatchService.find(tagBatchLookupTable.getTagBatchId());
		if(null != tagBatch) {
			tCodeD.setDomain(tagBatch.getDomain());
			tCodeD.setSubDomain(tagBatch.getSubDomain());
		}
		tagBatchLookupTable.setCurrentCount(totalTagCount + tagBatchLookupTable.getCurrentCount());
		try {
			tagBatchLookupTableDao.save(tagBatchLookupTable);
		} catch (Exception e) {
			throw new ServiceException("update tagBatchLookupTable error!",e);
		}
		return tCodeD;
	}

	@Override
	public void create(List<TagBatch> tagBatchs) {
		List<TagBatchLookupTable> tagBatchLookupTables = new Vector<>();
		TagBatchLookupTable table = null;
		if(null != tagBatchs &&! tagBatchs.isEmpty()) {
			for (TagBatch tagBatch : tagBatchs) {
				table = new TagBatchLookupTable();
				table.setEseId(tagBatch.getEseId());
				table.setType(tagBatch.getType());
				table.setSeqnumStart(tagBatch.getSeqnumStart());
				table.setSeqnumEnd(tagBatch.getSeqnumEnd());
				table.setTagBatchId(tagBatch.getId());
				tagBatchLookupTables.add(table);
			}
			tagBatchLookupTableDao.save(tagBatchLookupTables);
		}
	}

}
